package com.revengemission.sso.oauth2.server.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.ParserConfig;
import com.lvmoney.common.exceptions.BusinessException;
import com.lvmoney.common.exceptions.CommonException;
import com.lvmoney.redis.service.BaseRedisService;
import com.revengemission.sso.oauth2.server.constant.Oauth2ServerConstant;
import com.revengemission.sso.oauth2.server.ro.Oauth2ClientRo;
import com.revengemission.sso.oauth2.server.vo.OauthClient;
import com.revengemission.sso.oauth2.server.vo.UserInfo;
import com.revengemission.sso.oauth2.server.ro.Oauth2ClientDetailRo;
import com.revengemission.sso.oauth2.server.ro.Oauth2UserRo;
import com.revengemission.sso.oauth2.server.service.Oauth2RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Service;

/**
 * @describe：
 * @author: lvmoney /xxxx科技有限公司
 * @version:v1.0 2019年1月18日 上午11:28:35
 */
@Service
public class Oauth2RedisServiceImpl implements Oauth2RedisService {

    @Autowired
    BaseRedisService baseRedisService;

    @Override
    public void userDetails2Redis(Oauth2UserRo oauth2UserRo) {
        baseRedisService.addMap(Oauth2ServerConstant.REDIS_FRAME_USER_DETAILS_NAME, oauth2UserRo.getData(), oauth2UserRo.getExpire());
    }

    public UserInfo getOauth2UserVo(String username) {
        UserInfo userInfo = (UserInfo) baseRedisService.getValueByMapKey(Oauth2ServerConstant.REDIS_FRAME_USER_DETAILS_NAME, username);
        if (userInfo == null) {
            throw new BusinessException(CommonException.Proxy.OAUTH2_USER_DETAIL_NO_EXIST);
        }
        return userInfo;
    }

    @Override
    public ClientDetails getClientDetailsByClientId(String clientId) {
        Object obj = baseRedisService.getValueByMapKey(Oauth2ServerConstant.REDIS_FRAME_CLIENT_DETAILS_NAME, clientId);
        BaseClientDetails baseClientDetails = JSON.parseObject(obj.toString(), new TypeReference<BaseClientDetails>() {
        });
        if (baseClientDetails == null) {
            throw new BusinessException(CommonException.Proxy.OAUTH2_CLIENT_DETAIL_NO_EXIST);
        }
        return baseClientDetails;
    }

    @Override
    public void clientDetails2Redis(Oauth2ClientDetailRo oauth2ClientDetailRo) {
        baseRedisService.addMap(Oauth2ServerConstant.REDIS_FRAME_CLIENT_DETAILS_NAME, oauth2ClientDetailRo.getData(), oauth2ClientDetailRo.getExpire());
    }

    @Override
    public void client2Redis(Oauth2ClientRo oauth2ClientRo) {
        baseRedisService.addMap(Oauth2ServerConstant.REDIS_FRAME_CLIENT_NAME, oauth2ClientRo.getData(), oauth2ClientRo.getExpire());

    }

    @Override
    public OauthClient getClientByClientId(String clientId) {
        Object obj = baseRedisService.getValueByMapKey(Oauth2ServerConstant.REDIS_FRAME_CLIENT_NAME, clientId);
        OauthClient oauthClient = JSON.parseObject(obj.toString(), new TypeReference<OauthClient>() {
        });
        if (oauthClient == null) {
            throw new BusinessException(CommonException.Proxy.OAUTH2_CLIENT_DETAIL_NO_EXIST);
        }
        return oauthClient;
    }

}
